package com.ymatou.socialbizer.testcase;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONObject;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.socialbizer.parameter.KeyWordBean;
import com.ymatou.socialbizer.service.KeyWordCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.tag.P2;
import com.ymttest.database.mongo.NoteMongoWapper;

public class Ts_KeyWord {
	private static KeyWordBean keyWordBean;
	private static KeyWordCall keyWordCall;
	
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("笔记关键词搜索接口");
	}

	@Before
	public void caseUp() {
		keyWordBean=new KeyWordBean();
		keyWordCall=new KeyWordCall();
		
	}

	
	@AfterClass
	public static void tearDown() {
		Logger.generateResult(keyWordCall.getOpurl());
		//Logger.generateResult();
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	
	@Test
	@Category(P2.class)
	@TestCase
	public void Tc_KeyWord_001() {
		Logger.start(true,"笔记关键词搜索接口- 验证接口返回公共数据");
		
		try {
			//设置参数
			String keywordString  = "日本";
			int limit = 5;
			keyWordBean.setQuery("keyword(tags content,"+keywordString+")");
			keyWordBean.setFl("noteid,tags,content");
			keyWordBean.setLimit("0,"+limit);
			keyWordBean.setInfo("userid:111,cookieid:111555555555,yid:abcde");
			keyWordCall.setData(keyWordBean);
			keyWordCall.callService();
			
		//	JSONArray hits = keyWordCall.getHits();
			
			Logger.verifyEquals("OK",keyWordCall.getCommonInfo("status") , "检查接口状态值");
			Logger.verifyNotNull(keyWordCall.getCommonInfo("took"), "took不为空");
			Logger.verifyEquals(false, keyWordCall.getCommonInfoBoo("downgrade"), "验证downgrade返回false");
			Logger.verifyEquals(false, keyWordCall.getCommonInfoBoo("timeout"), "验证timeout返回flase");
			Logger.verifyNotNull(keyWordCall.getCommonInfo("hostip"), "服务器ip不为空");
			//Logger.verifyNotNull(keyWordCall.getCommonInfo("algorithm"), "algorithm不为空");
			
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P2.class)
	@TestCase
	public void Tc_KeyWord_002() {
		Logger.start(true,"笔记关键词搜索接口- 当有多条数据返回时候验证数据是否重复 ");
		
		try {
			//设置参数
			String keywordString  = "日本";
			int limit = 5;
			keyWordBean.setQuery("keyword(tags content,"+keywordString+")");
			keyWordBean.setFl("noteid,tags,content");
			keyWordBean.setLimit("0,"+limit);
			keyWordBean.setInfo("userid:111,cookieid:111555555555,yid:abcde");
			keyWordCall.setData(keyWordBean);
			keyWordCall.callService();
			
			JSONArray hitsAry = keyWordCall.getHits();
			
			List<String> list = new ArrayList<String>();

			for (int i = 1; i <= hitsAry.length(); i++) {
				
				String ids = hitsAry.get(i - 1).toString();
				JSONObject json = new JSONObject(ids);
				
				String id = json.getString("noteid");
				
				if (list.contains(id) == true) {

					Logger.verifyEquals(false, list.contains(id), "验证第" + i+ "条返回数据的noteid存在重复值" + id);

				}else{
					Logger.verifyEquals(false, list.contains(id), "验证第" + i+ "条返回数据的noteid不存在重复值" + id);
					list.add(id);
				}
			
			}
		
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P2.class)
	@TestCase
	public void Tc_KeyWord_003() {
		Logger.start(true,"笔记关键词搜索接口- 是否匹配单个关键字搜索");
		
		try {
			//设置参数
			String keywordString  = "日本";
			int limit = 5;
			keyWordBean.setQuery("keyword(tags content,"+keywordString+")");
			keyWordBean.setFl("noteid,tags,content");
			keyWordBean.setLimit("0,"+limit);
			keyWordBean.setInfo("userid:111,cookieid:111555555555,yid:abcde");
			keyWordCall.setData(keyWordBean);
			keyWordCall.callService();
			
			JSONArray hitsAry = keyWordCall.getHits();
			
			List<String> list = new ArrayList<String>();

			for (int i = 1; i <= hitsAry.length(); i++) {
				
				String ids = hitsAry.get(i - 1).toString();
				JSONObject json = new JSONObject(ids);
				
				String content = json.getString("content");
				JSONArray tags = json.getJSONArray("tags");
				
				//检查content里是否包含  keyword关键字
				int tageInt = 0;
				 boolean ifContent = content.contains(keywordString);
				 if(ifContent==true){
					 Logger.verifyEquals(true, ifContent, "content中包含"+keywordString+"关键字");
					 
				 }else{
					 for (int j = 0; j < tags.length(); j++) {
							String tagString = tags.get(j).toString();
							boolean ifTags= tagString.contains(keywordString);
							if(ifTags==true){
								 tageInt=1;
								
							}
						}
					 Logger.verifyEquals(1, tageInt, "tags中是否包含"+keywordString+"关键字");
				 }
				 
				
			}
			
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P2.class)
	@TestCase
	public void Tc_KeyWord_004() {
		Logger.start(true,"笔记关键词搜索接口- 是否匹配多个关键字搜索");
		
		try {
			//设置参数
			String keywordString  = "日本";
			String keywString2 = "人民币";
			int limit = 5;
			keyWordBean.setQuery("keyword(tags content,"+keywordString+" "+keywString2+")");
			keyWordBean.setFl("noteid,tags,content");
			keyWordBean.setLimit("0,"+limit);
			keyWordBean.setInfo("userid:111,cookieid:111555555555,yid:abcde");
			keyWordCall.setData(keyWordBean);
			keyWordCall.callService();
			
			JSONArray hitsAry = keyWordCall.getHits();
			
			List<String> list = new ArrayList<String>();

			for (int i = 1; i <= hitsAry.length(); i++) {
				
				String ids = hitsAry.get(i - 1).toString();
				JSONObject json = new JSONObject(ids);
				
				String content = json.getString("content");
				JSONArray tags = json.getJSONArray("tags");
				
				//检查content里是否包含  keyword关键字
				int tageInt = 0;
				int tageInt1 =0;
				int tageInt2 = 0;
				 boolean ifContent = content.contains(keywordString);
				 boolean ifContent2 = content.contains(keywString2);
				 if(ifContent==true && ifContent2==true){
					 Logger.verifyEquals(true, ifContent, "content中即包含<"+keywordString+">也包含<"+keywString2+">关键字");
					 
				 }else{
					 for (int j = 0; j < tags.length(); j++) {
							String tagString = tags.get(j).toString();
							boolean ifTags= tagString.contains(keywordString);
							boolean ifTags2 = tagString.contains(keywString2);
						
							if(ifTags==true ){
								 tageInt1=1;
								
							}
							if(ifTags2==true){
								tageInt2=1;
							}
							
							if(tageInt1==1 && tageInt2==1){
								tageInt=1;
							}
						}
					 Logger.verifyEquals(1, tageInt, "tags中是否即包含<"+keywordString+">也包含<"+keywString2+">关键字");
				 }
				 
				
			}
			
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P2.class)
	@TestCase
	public void Tc_KeyWord_005() {
		Logger.start(true,"笔记关键词搜索接口- 验证返回笔记是否都为已通过审核笔记");
		
		try {
			//设置参数
			String keywordString  = "日本";
			int limit = 5;
			keyWordBean.setQuery("keyword(tags content,"+keywordString+")");
			keyWordBean.setFl("noteid,tags,content");
			keyWordBean.setLimit("0,"+limit);
			keyWordBean.setInfo("userid:111,cookieid:111555555555,yid:abcde");
			keyWordCall.setData(keyWordBean);
			keyWordCall.callService();
			
			JSONArray hitsAry = keyWordCall.getHits();
			
			//通过noteid查mongo中笔记是否为通过审核笔记
			NoteMongoWapper notes = new NoteMongoWapper();
			 
			
			for (int i = 1; i <= hitsAry.length(); i++) {
					
				String ids = hitsAry.get(i - 1).toString();
				JSONObject json = new JSONObject(ids);
				int noteid = json.getInt("noteid");
				
				ArrayList noteInfo = notes.getNoteBynoteId(noteid);
				
				Logger.verifyEquals(false, noteInfo.isEmpty(), "noteid为"+noteid+"的笔记是否满足关联订单且已通过审核");
				
				
			}
				
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
}
